// pages/scanningpage/scanninglist/scanninglist.js
var Tool = require("../../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focusList: [],
    isOpenList: [],
    detailList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  loadData: function () {
    var that = this
    that.data.focusList = []
    that.data.isOpenList = []
    that.data.detailList = []
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
      data: {
        tel: userInfo.cTel,
        pwd: userInfo.UserPwd,
        act: 'getl',
      },
      method: 'GET',
      success: function (res) {
        var data = res.data
        console.log(data)
        if (data.code == '0') {
          console.log('接口调用成功，code=0')
          if (data.data.length > 0) {
            console.log('有订单数据')

            for (var i = 0; i < that.data.focusList.length; i++) {
              that.data.isOpenList.push(false)
              that.data.detailList.push([])
            }

            that.setData({
              focusList: that.data.focusList.concat(data.data),
              isOpenList: that.data.isOpenList,
              detailList: that.data.detailList,
            });

            console.log(that.data.isOpenList)
            console.log(that.data.detailList)
          }
        } else {
          //返回code不为0，说明有错误
          console.log('接口调用成功，但是操作有误，返回值不为0，打印错误信息')
        }
      },
      fail: function () {
        //接口加载失败
        console.log('加载接口错误');
      }
    })
  },

  testMap: function (e) {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');
    console.log(e)
    var id = e.currentTarget.dataset.id
    // 判断是否打开
    if (that.data.isOpenList[id]) {
      that.data.isOpenList[id] = false
      that.setData({
        detailList: that.data.detailList,
        isOpenList: that.data.isOpenList
      });
    } else {
      that.data.isOpenList[id] = true
      //获取数据
      var realid = e.currentTarget.dataset.realid
      wx.request({
        url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
        data: {
          tel: userInfo.cTel,
          pwd: userInfo.UserPwd,
          act: 'getd',
          atnid: realid
        },
        method: 'GET',
        success: function (res) {
          var data = res.data
          console.log(data)
          if (data.code == '0') {
            console.log('接口调用成功，code=0')
            if (data.data.length > 0) {
              console.log('有订单数据')
              for (var i = 0; i < data.data.length; i++) {
                data.data[i].UseCarStartTime = data.data[i].UseCarStartTime.substring(0, 10)
              }
              //数据存入list中备用
              that.data.detailList[id] = data.data
              // 渲染界面
              that.setData({
                detailList: that.data.detailList,
                isOpenList: that.data.isOpenList
              });
              console.log(that.data.isOpenList)
              console.log(that.data.detailList)
            } else {
              // 渲染界面
              that.setData({
                detailList: that.data.detailList,
                isOpenList: that.data.isOpenList
              });
            }
          } else {
            //返回code不为0，说明有错误
            console.log('接口调用成功，但是操作有误，返回值不为0，打印错误信息')
          }
        },
        fail: function () {
          //接口加载失败
          console.log('加载接口错误');
        }
      })
    }
  },

  //取消关注
  cancelFocus: function (e) {
    var that = this
    //获取数据
    var realid = e.currentTarget.dataset.realid
    wx.showModal({
      title: '提示',
      content: '是否取消关注该订单',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var userInfo = wx.getStorageSync('userInfo');
          wx.request({
            url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
            data: {
              tel: userInfo.cTel,
              pwd: userInfo.UserPwd,
              act: 'atn_cancel',
              atnid: realid
            },
            method: 'GET',
            success: function (res) {
              var data = res.data
              console.log(data)
              if (data.code == '0') {
                console.log('接口调用成功，code=0')
                that.loadData()
              } else {
                //返回code不为0，说明有错误
                console.log('接口调用成功，但是操作有误，返回值不为0，打印错误信息')
              }
            },
            fail: function () {
              //接口加载失败
              console.log('加载接口错误');
            }
          })
        } else {
          console.log('用户点击取消')
        }

      }
    })
  },

  toDetail: function (e) {
    var that = this
    console.log(e)
    var IDD = e.currentTarget.dataset.idd
    wx.navigateTo({
      url: '../../../pages/passengers/passengerDe?IDD=' + IDD,
    })
  }
})